//
//  AppDelegate.m
//  DFront
//
//  Created by lanou on 15/10/26.
//  Copyright © 2015年 xuzenghui. All rights reserved.
//

#import "AppDelegate.h"
#import "DFBaseViewController.h"
#import "DFForumController.h"
#import "DFProductsViewController.h"
#import "DFUserViewController.h"
#import "DFMainHeadlineTableViewController.h"
#import "UIColor+DFThemeColor.h"
#import "UMSocial.h"
#import "Reachability.h"
#import "UIBezierPath+ZJText.h"
@interface AppDelegate ()
@property (nonatomic,strong) UIImageView *imageView;
@property (strong, nonatomic) NSMutableDictionary *attrs;
@property (strong, nonatomic) CAShapeLayer *shapeLayer;
@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
    self.window.backgroundColor = [UIColor whiteColor];
    
   //  设置友盟
    [UMSocialData setAppKey:@"56342e7467e58e0448004498"];
    
    UITabBarController *tabBar = [[UITabBarController alloc]init];
    tabBar.tabBar.tintColor = [UIColor themeColor];
    
    DFForumController *forumVC = [[DFForumController alloc]init];
    UINavigationController *forumNC = [[ UINavigationController alloc]initWithRootViewController:forumVC];
    
    forumNC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"论坛" image:[UIImage imageNamed:@"disscuss"]  selectedImage:[UIImage imageNamed:@"disscuss"]];
    
    DFProductsViewController *productsVC = [[DFProductsViewController alloc]init];
    UINavigationController *productsNC = [[UINavigationController alloc]initWithRootViewController:productsVC];
    productsNC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"产品" image:[UIImage imageNamed:@"product"] selectedImage:[UIImage imageNamed:@"product"]];

    
    DFUserViewController *userVC = [[DFUserViewController alloc]init];
    UINavigationController *userNC = [[UINavigationController alloc]initWithRootViewController:userVC];
    
    userNC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"用户" image:[UIImage imageNamed:@"user"] selectedImage:[UIImage imageNamed:@"user"]];

    
    DFMainHeadlineTableViewController *headLineVC = [[DFMainHeadlineTableViewController alloc]init];
    UINavigationController *headlineNC = [[UINavigationController alloc]initWithRootViewController:headLineVC];
    
    headlineNC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"资讯" image:[UIImage imageNamed:@"message"]  selectedImage:[UIImage imageNamed:@"message"]];
    
    NSArray *arr = @[headlineNC,forumNC,productsNC,userNC];
    
    tabBar.viewControllers = arr;
    self.window.rootViewController = tabBar;
    [self.window makeKeyAndVisible];
    
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"everLaunched"]) {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"everLaunched"];
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"];
        [self appBeginAnimation];

    }
    else{
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"firstLaunch"];
    }
    
    Reachability * reach = [Reachability reachabilityWithHostname:@"www.baidu.com"];
    //判断网络
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
    
    [reach startNotifier];
    
       return YES;
}


-(CAShapeLayer *)shapeLayer
{
    if (_shapeLayer == nil)
    {
        _shapeLayer = [CAShapeLayer layer];
        
        CGSize size = self.imageView.frame.size;
        CGFloat height = 250;
        
        _shapeLayer.frame = CGRectMake(0, (size.height - height)/2, size.width , height);
        _shapeLayer.geometryFlipped = YES;
        _shapeLayer.strokeColor = [UIColor cyanColor].CGColor;
        _shapeLayer.fillColor = [UIColor clearColor].CGColor;;
        _shapeLayer.lineWidth = 2.0f;
        _shapeLayer.lineJoin = kCALineJoinRound;
    }
    return _shapeLayer;
}
-(NSMutableDictionary *)attrs
{
    if (_attrs == nil)
    {
        _attrs = [[NSMutableDictionary alloc] init];
        [_attrs setValue:[UIFont boldSystemFontOfSize:36] forKey:NSFontAttributeName];
    }
    return _attrs;
}


- (void)appBeginAnimation
{
    self.imageView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.imageView.image = [UIImage imageNamed:@"ee"];
    
    //添加到场景
    [self.window addSubview:self.imageView];
    
    //放到最顶层;
    [self.window bringSubviewToFront:self.imageView];
    
    //开始设置动画;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:5];
    [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:self.window cache:YES];
    [UIView setAnimationDelegate:self];
    //這裡還可以設置回調函數;
    //[UIView setAnimationDidStopSelector:@selector(startupAnimationDone:finished:context:)];
    self.imageView.alpha = 0.0;
    [UIView commitAnimations];

    [self.imageView.layer addSublayer:self.shapeLayer];
    // 添加动画写字
    UIBezierPath *path = [UIBezierPath zjBezierPathWithText:@"数码前线" attributes:self.attrs];
    self.shapeLayer.bounds = CGPathGetBoundingBox(path.CGPath);
    self.shapeLayer.path = path.CGPath;
    
    [self.shapeLayer removeAllAnimations];
    
    CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    pathAnimation.duration = 2;
    pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
    pathAnimation.toValue = [NSNumber numberWithFloat:1.0f];
    [self.shapeLayer addAnimation:pathAnimation forKey:@"strokeEnd"];
    
    [self performSelector:@selector(removeImage) withObject:nil afterDelay:3];
}


- (void)removeImage{
    [self.imageView removeFromSuperview];
}


//通知

-(void)reachabilityChanged:(NSNotification*)note

{
    Reachability * reach = [note object];
    
    if([reach isReachable])
        
    {
        NSLog(@"Notification Says Reachable");
    }
    
    else
        
    {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"好像没网了==!" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        
        [alertView show];
        
        NSLog(@"Notification Says Unreachable");
    }
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
